ArcPy and ArcGIS - Second Edition by Silas Toms & Dara O’Beirne

ArcPy and ArcGIS - Second Edition by Silas Toms & Dara O’Beirne

Author:Silas Toms & Dara O’Beirne [Toms, Silas]
Language: eng
Format: azw3
Publisher: Packt Publishing
Published: 2017-08-04T04:00:00+00:00


Adding dynamic components to the script

To start making the script into a script tool, we should first copy the script that we created in Chapter 4, ArcPy Geometry Objects and Cursors, Chapter4_1.py as Chapter5_1.py in a new folder called Chapter5. We can then start replacing the hard-coded variables with dynamic variables using arcpy.GetParameterAsText. There is another ArcPy method called GetParameter, which accepts the inputs as an object, but, for our purpose, we need to use GetParameterAsText.

By adding arcpy.GetParameterAsText and arcpy.AddMessage to the script, we will take the first step towards creating a script tool. Care must be taken to ensure that the variables created from the dynamic parameters are in the correct order, as reordering them can be time consuming. Once the parameters are added to the script, and hard-coded portions of the script are replaced with variables, the script is ready to become a script tool.

First, move all of the variables that are hard-coded to the top of the script. Then, replace all of the assigned values with arcpy.GetParameterAsText, making them dynamic values. Each parameter is referred to using zero-based indexing; however, they are passed to a function individually instead of as a member of a list, as shown next:

#Chapter 5_ScriptTool.py

import arcpy, csv

busStops = arcpy.GetParameterAsText(0)

censusBlocks2010 = arcpy.GetParameterAsText(1)

censusBlockField = arcpy.GetParameterAsText(2)

csvname = arcpy.GetParameterAsText(3)

headers = arcpy.GetParameterAsText(4).split(',')

sql = arcpy.GetParameterAsText(5)

keyfields = arcpy.GetParameterAsText(6).split(';')



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.